﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using CuaHangMayTinh.BusinessEntities;
using CuaHangMayTinh.BusinessLogicLayer;
using CuaHangMayTinh.Utilities;
using DevComponents.DotNetBar.Metro;

namespace CuaHangMayTinh.PresentationLayer
{
    public partial class FormTaiKhoan : Form
    {
        private DataTable Table;
        
        public FormTaiKhoan()
        {
            InitializeComponent();
        }

        private void FormTaiKhoan_Load(object sender, EventArgs e)
        {
            var tbNhanVien = NhanVienManager.Find_All(false);
            Table = tbNhanVien;
            LoadListView(dataGridViewX_NhanVien, tbNhanVien, "TenNV");
            toolStriptxt_pass.TextBox.PasswordChar = '*';
        }

        private void dataGridViewX_NhanVien_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridViewX_NhanVien.SelectedRows.Count > 0)
            {
                var row = (int)dataGridViewX_NhanVien.SelectedRows[0].Cells[0].Value;
                var tbtaikhoan =TaiKhoanManager.Find_MaNV(row);
                LoadListView(dataGridView_User,tbtaikhoan,"UserName");
            }
        }

        private void LoadListView(DataGridView dataGrid, DataTable dt, string ten)
        {
            if (dt.Rows.Count>0)
            {
                if (!dt.Columns.Contains("STT"))
                {
                    dt.Columns.Add("STT");
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        dt.Rows[i]["STT"] = i + 1;
                    }
                }
                dataGrid.DataSource = dt;
                for (int i = 0; i < dataGrid.Columns.Count; i++)
                {
                    dataGrid.Columns[i].Visible = false;
                }
                dataGrid.Columns["STT"].DisplayIndex = 0;
                dataGrid.Columns["STT"].Visible = true;
                dataGrid.Columns[ten].Visible = true;
            }
            else
            {
                dataGrid.DataSource = null;
            }
        }

        private void thêmToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStripTxt_user.Enabled = true;
            toolStriptxt_pass.Enabled = true;
            toolStripbtn_them.Enabled = true;
        }

        private bool CheckInput()
        {
            if (StringHelper.IsNullorEmpty(toolStripTxt_user.Text))
            {
                MessageBox.Show("Vui lòng nhập tên tài khoản vào ! ");
                return false;
            }
            if (StringHelper.IsNullorEmpty(toolStriptxt_pass.Text))
            {
                MessageBox.Show("Vui lòng nhập mật khẩu vào ! ");
                return false;
            }
            return true;
        }

        private void toolStripbtn_PhucHoi_Click(object sender, EventArgs e)
        {
            var row = (int)dataGridViewX_NhanVien.SelectedRows[0].Cells[0].Value;
            if (CheckInput())
            {
                if (TaiKhoanManager.Insert(new TaiKhoanEntity(toolStripTxt_user.Text, toolStriptxt_pass.Text, row, 0)) >
                    0)
                {
                    MessageBox.Show("Thêm tài khoản thành công !");
                    toolStripTxt_user.Enabled = false;
                    toolStriptxt_pass.Enabled = false;
                    toolStripbtn_them.Enabled = false;
                    dataGridViewX_NhanVien_RowEnter(null, null);
                }
                else
                {
                    MessageBox.Show("Thêm tài khoản thất bại");
                }
            }
        }

        private void toolStriptb_timnhanh_TextChanged(object sender, EventArgs e)
        {
            DataTable dt = Table;
            if (!StringHelper.IsNullorEmpty(toolStriptextbox_timnhanh.Text))
            {
                DataTable tb = DataUtil.TimkiemTheo(dt, "TenNV", toolStriptextbox_timnhanh.Text);
                if (tb != null)
                {
                    tb.Columns.RemoveAt(tb.Columns.Count - 1);
                    LoadListView(dataGridViewX_NhanVien, tb, "TenNV");
                }
                else
                {
                    dataGridViewX_NhanVien.DataSource = null;
                }
            }
            else
            {
                LoadListView(dataGridViewX_NhanVien, Table, "TenNV");
            }
        }

        private void xóaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dataGridView_User.SelectedRows.Count > 0)
            {
                var row = (string) dataGridView_User.SelectedRows[0].Cells[1].Value;
                if (TaiKhoanManager.Delete(row))
                {
                    MessageBox.Show("Xóa tài khoản thành công !");
                    dataGridViewX_NhanVien_RowEnter(null, null);
                }
                else
                {
                    MessageBox.Show("Xóa tài khoản thất bại");
                }
            }
        }

    }
}
